C++: A Start: Compiling and Types (variables and constants)
Copyright ⌐ 1997 RaMPaGe
ref: Teach Yourself C++ in 21 Days by Jesse Liberty
Document Courtesy Of The Immortal Descendants
C++ is what is known as an Object Oriented language. Later you will fully
understand what this means. But for now, understand that it makes C++ one
of the most versatile and widely used languages today. It is the
programming industry standard.
The first thing you need in order to program in C++ is a compiler.
My personal recommendation is Microsoft Visual C++ 4.0 or higher. It is a
good beginner's compiler, and you need not use the visual aspect of it.
Also it includes templates in it for programming in non-Windows
applications. To get an understanding of C++ you must first understand
basically how a compiler works.
After writing your source code, the file is saved as a .cpp file.
This is the source code file. Then you compile it. This converts the code
to binary so the computer can understand what you want it to do. Computers
don't know code, they only understand binary, because all the are, are
millions of diodes put in a chip. So all they no is either there is a
voltage in a certain point, or there isn't one. Therefore they need binary
to understand the code (binary is either a one or a zero). At compile time
the compiler also debugs the program. That is, it checks your code to make
sure there are no typos with the type declarations, the way it is used, etc
(the syntax). Then after you compile you have a .obj file. The next step is
to link it. Linking a .obj file basically connects it to a library file, so
the executable program knows what all the code is supposed to do. After this
is done, the compiler debugs it again. Anytime during both debugging
processes, if you have a bug that the debugger finds, you must correct the
problem first before continuing in the process. The end product is an
executable program that you can run.
Now that you understand basically how a compiler works, lets move
on to some of the types you can declare when making a program. A type is
either a variable or a constant, the most basic parts of a C++ program, the
raw data. Now when making a program in C++, memory is allocated to data
based on the type of data it is. There are several types of data. They
include: char, short int, long int, float, and double. The type will tell
the computer how much memory to set aside for each piece of data. An int is
either 2 bytes or 4 bytes, depending on whether it is a short int or a long
int. Int by itself will either be defaulted to a short int or a long int
depending on the computer you have. On my computer, int by itself is
defaulted to a long int. Now a short int takes 2 bytes of memory. A long int
takes 4 bytes. A char takes 1 byte, a float takes 4 bytes, and a double
takes 8 bytes. The type also declares what kind of data can be stored there.
The reason for having all these different types, is you may not wish to use
fractions, or numbers (you might want to use words, such as a name), or you
may want something accurate to several decimal places, or use very
large numbers. In addition to this, an int may be signed or unsigned.
Signed or unsigned means whether or not it should include negative numbers.
Signed allows for negative numbers, unsigned does not. By looking at the
following table, you can see what u can put in each type.
Type Values
Unsigned short int an integer 0 to 65,535
short int an integer -32,768 to 32,767
unsigned long int an integer 0 to 4,294,967,295
long int an integer -2,147,483,648 to 2,147,483,647
char 256 character values
float -1.2e38 to 3.4e38
double -2.2e308 to 1.8e308
OK, so now you know types and what can be used in each type, but
how do you actually use these in a program? Well when using C++ you must
first declare the variable, then either initialize right away, or later. A
constant must always be initialized immediately. You may name your
variable whatever you like, except for a few keywords. I will write a
document with the C++ keywords seperate to this one. Now for some small
snipets of code to demonstrate how you declare a variable and initialize it.
unsigned short int MyAge;
in the previous example, I declared a variable to be an unsigned
integer and named it MyAge. This is the declaration.
MyAge = 24;
In this example, I initialized the variable MyAge to a value of 24.
Note that in both cases, the use of the semicolin (;) after each statement.
This is essential when declaring a variable...without it you will receive a
compile time error during debugging. Also remember, you cannot initialize a
variable without declaring it first. Now here is an example of a literal
constant:
unsigned short int MyAge = 24;
Here I declared and initialized it in one statement. This assigns
it as a literal constant. There are also other types of constants, too.
These other types are called symbolic and enumerated.
To declare a literal constant, use the keyword const.
const unsigned short MyAge = 24;
Note that I declared and initialized it in the same line. You must
declare and initialize constants in the same line.
The last kind of constant, an enumerated constant is complex. It
basically allows you to declare a new type, and to define the fields of
that type. Here is an example:
enum COLOR { RED=1, BLUE, GREEN, WHITE=20, BLACK };
this statement declares a new type, color, and it makes red, blue,
green, white, and black symbolic constants of that type. It also assigns a
value of 1 to red and 20 to white. It will automatically assign the next
higher integer value to each of the following constants, until told to do
otherwise. So in the case above, red = 1, blue = 2, green = 3, white = 20,
and black = 21. Also you can declare variables using the type you declare
in the enumerated constant, but it should only be set to one of the values
defined in the constant.
This concludes the lesson on types and basic compiling operations.
-RaMPaGe-